# -*- coding: utf-8 -*-
# @Time    : 2018/12/12 9:54
import os
import sys
from datetime import datetime

path = os.path.abspath(os.path.dirname(os.getcwd()))
sys.path.append(path)
from creat_bar import config
from multiprocessing import Process, Queue, Pool
keyword = config.keyword

class insertBat(object):

    def __init__(self) -> None:
        self.B = 0
        super().__init__()

    def find_mongo(self):
        # 文件夹名字要改成docid
        zip_name = "one_carw"
        start_file_num = 0
        result = 0
        top_path = (config.top_path)%(zip_name)
        files_nr = ""
        with open('../bat/' + str(zip_name) + '.bat', 'a') as af:
            items = keyword.find()
            # while True:
            for item in items:
                try:
                    # item = keyword.find_and_modify({"state":0},{'$set': {"state":1}})
                    # if not item:
                    #     return
                    try:
                        path = item['path']
                        nr = str(path).split("/")[-1]
                        path = str(path).replace("/","\\")
                    except Exception as e:
                        print(e)
                        continue
                except Exception as e:
                    print(e)
                if not result % 100000:
                    parent_dir = (4 - len(str(start_file_num))) * '0' + str(start_file_num)
                    dir_path = '%s\\%s' % (top_path, parent_dir)
                    af.write('md %s\n' % dir_path)
                    start_file_num += 1
                new_path = '%s\\%s' % (dir_path,nr)
                # af.write('del %s'%(path))#删除文件
                files_nr += str('move %s %s\n' % (path, new_path))
                if not result % 3000:
                    new_time_one = datetime.now()
                    af.write(files_nr)  # 移动文件
                    files_nr = ""
                    if result:
                        print(zip_name, result,os.getpid())
                    else:
                        print(zip_name, 'start',os.getpid())
                    new_time = datetime.now()
                    time_l = (new_time - new_time_one).seconds
                    print("插入语句耗时:%s"%time_l)
                result += 1
            af.write(files_nr)
            print(result)
if __name__ == '__main__':
    yy = insertBat()
    trader = []
    for i in range(1):
        pr = Process(target=yy.find_mongo)
        pr.start()
        trader.append(pr)
    for i in trader:
        i.join()
    print('this is %s' % (os.getpid()))